package com.huluobo.a2212mdmdemo.pages

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2212mdmdemo.R
import com.huluobo.a2212mdmdemo.demo.MyBanner
import com.huluobo.apps.pages.AppsPage
import com.huluobo.lib_base.RoutePath

/**
 *  Created by LC on 2025/5/24.
 *  框架页面,在MainActivity中不放任何控件,导航之类的东西都在此页面
 */

//密封类,区分每个按钮需要对应生成的页面
sealed class NavItem(val route: String, val title: String, val img: Int) {
    data object MessagePage : NavItem(RoutePath.MESSAGE, "消息", R.drawable.message)
    data object HomePage : NavItem(RoutePath.HOME, "门户", R.drawable.home)
    data object MinePage : NavItem(RoutePath.MINE, "我的", R.drawable.mine)
}

@Composable
fun MainPage() {
    //底部导航的数据源
    val list = listOf(NavItem.MessagePage, NavItem.HomePage, NavItem.MinePage)
    //导航的控制器
    val navHostController = rememberNavController()

    //获取当前回退站的状态
    val stackEntryState by navHostController.currentBackStackEntryAsState()

    //获取回退站中所有的地址
    val destination = stackEntryState?.destination
    Scaffold(
        //脚手架中底部栏的位置,一般放置底部导航或者提交按钮
        bottomBar = {

            if (destination?.hierarchy?.any {
                    //通过拿到底部导航的地址来对比,如果包含底部导航的地址,就显示底部导航栏,否则就跳过不显示
                    //转化list中的地址
                    list.map { navItem ->
                        navItem.route
                        //如果包含底部导航的地址就为true
                    }.contains(it.route)
                } == true) {
                //导航栏
                NavigationBar {
                    list.forEach {
                        NavigationBarItem(selected = false, onClick = {
                            //点击底部导航时跳转到对应的路径
                            navHostController.navigate(it.route)
                        }, icon = {
                            //导航图标,直接从数据源中取出图标的id
                            Icon(
                                painter = painterResource(id = it.img),
                                contentDescription = "",
                                modifier = Modifier.size(30.dp)
                            )
                        }, label = {
                            //导航的标签
                            Text(text = it.title)
                        })
                    }
                }
            }
        }
    ) {
        MyNavHost(it = it, navHostController = navHostController)
    }
}